Hi,
I'm a bit rusty with gsap, and need some help.
I did a bit of vanilla js to pin a div for a moment, and it's working fine, (even though sometime it's a bit laggy on the first scroll) but since I'm using gsap and scroll trigger for something else on the same page, I though it would be better to use it too here, but I'm not sure how to do it.
The pinned div has an absolute position, with a 50% top and transform translateY -50%, and when you scroll it push is it down so it look fixed.
The cover_parallax div has a 200vh Height, so when it's scrolled to the bottom of the warper it stop.
here is the code :
window.addEventListener('scroll', checkContainerVisibility);
//////////////////////
function toggleFixed() {
isFixed = !isFixed;
if (isFixed) {
// Set initial position when becoming fixed
updateFixedPosition();
window.addEventListener('scroll', updateFixedPosition);
window.addEventListener('resize', updateFixedPosition);
} else {
// Clean up when no longer fixed
window.removeEventListener('scroll', updateFixedPosition);
window.removeEventListener('resize', updateFixedPosition);
}
}
// Function to update the position of the fixed element based on scroll and resize
function updateFixedPosition() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Adjust the top position of the fixed element
content.style.top = `calc( 50% + ${scrollTop}px`;
}
function checkContainerVisibility() {
var bounding = cover_parallax.getBoundingClientRect();
if ( bounding.bottom < 0 ) {
cover_parallax.classList.add('annimeover');
} else {
cover_parallax.classList.remove('annimeover');
}
}
How would you do the same with a gsap scroll trigger ?